<template>
  <div class="container">
    <el-dialog
      title="余料入库"
      :visible.sync="showDialog_copy"
      class="main_dialog dialog800"
      @closed="closeOrderDialog"
    >
      <el-form
        :inline="false"
        :model="MaterielInfo"
        class="demo-form-inline"
        ref="craftInfoRef"
        label-width="80px"
        key="userListFrom1"
      >
        <div>
          <div class="main_table table_container" style="min-height: 200px">
            <el-table
              ref="multipleTable"
              v-loading="listLoading"
              :data="MaterielInfo.list"
              element-loading-text="加载中"
              fit
              border
              highlight-current-row
            >
              <el-table-column label="序号" width="60">
                <template slot-scope="scope">
                  {{ scope.$index + 1 }}
                </template>
              </el-table-column>
              <el-table-column
                min-width="80"
                show-overflow-tooltip
                label="余料名称"
                prop="name"
              />
              <el-table-column label="单位" prop="spu_unit" />

              <el-table-column
                label="剩余余料"
                min-width="120"
                prop="unreceived_num"
              >
                <template slot-scope="scope">
                  <el-input
                    v-model="scope.row.unreceived_num"
                    :class="'num_list' + scope.$index"
                    placeholder="剩余余料"
                    @change="numInput(scope.row)"
                  ></el-input>
                </template>
              </el-table-column>
              <el-table-column
                label="操作"
                fixed="right"
                align="center"
                prop="order_list"
              >
                <template slot-scope="scope">
                  <el-link type="warning" @click="CraftDelete(scope)">
                    删除</el-link
                  >
                </template></el-table-column
              >
            </el-table>
          </div>
        </div>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <div style="margin-right: 15px">
          <p class="tips">请删除剩余数量为空的余料</p>
          <p class="tips">若全部余料为空请点击【没有余料】</p>
        </div>

        <el-button
          type="primary"
          :loading="saveloading"
          @click="preserve(0)"
          v-repeatClick
          >入库</el-button
        >
        <el-button
          type="primary"
          :loading="saveloading"
          @click="preserve(1)"
          v-repeatClick
          >没有余料</el-button
        >
        <el-button @click="showDialog_copy = false">取消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import {
  process_getList,
  process_lastStorage
} from "@/api/sorting/processOrder";

export default {
  name: "",
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    id: {
      type: Number | String,
      default: ""
    }
  },
  computed: {
    showDialog_copy: {
      get: function() {
        return this.showDialog;
      },
      set: function(newval) {
        this.$emit("update:showDialog", newval);
      }
    }
  },
  data() {
    return {
      listLoading: false,
      saveloading: false,
      // 领取物料
      // showDialog: false,
      MaterielInfo: {
        list: []
      },
      delete_ids: []
    };
  },
  methods: {
    preserve(type) {
      let message = "";
      switch (type) {
        case 0:
          message = "确认入库吗（余料入库只能入一次）？";
          break;
        case 1:
          message = "确认没有余料，此操作不可回退？（警告）";
          break;
      }
      this.$confirm(message, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this.process_lastStorage(type);
        })
        .catch(() => {});
    },
    process_lastStorage(type) {
      let arr = [];
      this.saveloading = true;
      this.MaterielInfo.list.map(e => {
        arr.push({
          id: e.id,
          num: e.unreceived_num,
          goods_name: e.name,
          goods_spu_id: e.spu.id,
          goods_id: e.goods.id,
          cost: e.inventory_price
        });
      });
      let data = {
        status: type,
        id: this.id,
        list: JSON.stringify(arr)
      };
      process_lastStorage(data).then(res => {
        if (res.errcode == 0) {
          this.$message.success("入库成功");
          this.showDialog_copy = false;
          this.$emit("refresh");
        } else {
          this.$message.error(res.errmsg);
        }
        this.saveloading = false;
      });
    },
    closeOrderDialog() {
      this.MaterielInfo.list = [];
      this.delete_ids = [];
      this.listLoading = false;
      this.saveloading = false;
    },
    // 删除
    CraftDelete(scope) {
      let index = scope.$index;
      this.delete_ids.push(scope.row.id);
      this.MaterielInfo.list.splice(index, 1);
    },
    numInput(row) {
      row.unreceived_num = row.unreceived_num.replace(/[^0-9.]/g, "");
    },
    //获取列表
    process_getList() {
      this.listLoading = true;
      const data = {
        id: this.id
      };
      let unreceived_num = 0;
      process_getList(data).then(res => {
        if (res.errcode == 0) {
          res.data.list.map(e => {
            e.name = `${e.goods.name}【${e.spu.spu_name.title}】`;
            e.spu_unit = `${e.spu.spu_unit.title}`;
            // e.unreceived_num = this.$toFixed(
            // 	parseFloat(e.num) - parseFloat(e.received_num)
            // );
            e.unreceived_num = " ";
          });
          this.MaterielInfo.list = res.data.list;
        } else {
          this.$message.error(res.errmsg);
        }
        this.listLoading = false;
      });
    }
  }
};
</script>
<style lang="scss" scoped>
.add_goods_container {
  display: flex;
  align-items: center;
  padding: 0 0 0 0px;
  li {
    display: flex;
    align-items: center;
    span {
      padding-right: 10px;
      display: block;
      width: 60px;
      text-align: right;
      em {
        padding: 0 5px;
        color: red;
      }
    }
    .width40 {
      width: 60px;
    }
    &:last-child {
      margin-left: 10px;
    }
  }
}
>>> .table_container .el-table--small td .cell {
  height: auto;
}
.icon {
  margin-left: 5px;
  vertical-align: baseline;
}
/deep/ .el-input-number .el-input__inner {
  text-align: left;
}
.dialog-footer {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  .tips {
    font-size: 12px;
    color: red;
  }
}
</style>
